package com.ks.winterwarmimage.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.ks.winterwarmimage.R;
import com.ks.winterwarmimage.utils.BasicsUtil;

import java.util.List;

public class AccountAdapter extends BaseAdapter {
    private Context context;
    private List<Account> accountIds;
    private boolean menuAdapter;
    public AccountAdapter(Context context, List<Account> accountIds){
        this(context, accountIds, false);
    }
    public AccountAdapter(Context context, List<Account> accountIds, boolean menuAdapter){
        this.context=context;
        this.accountIds=accountIds;
        this.menuAdapter=menuAdapter;
    }
    @Override
    public int getCount() {
        return accountIds != null ? accountIds.size() : 0;
    }

    @Override
    public Account getItem(int position) {
        return accountIds != null ? accountIds.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 设置单选项
     * @param position 需要选择的项目位置
     */
    public void selectItem(int position) {
        for (int i = 0; i < accountIds.size(); i++) {
            if (i == position) {
                accountIds.get(i).isSelected=true;
            } else {
                accountIds.get(i).isSelected=false;
            }
        }
        notifyDataSetChanged();
    }
    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.account_view, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Account account = accountIds.get(position);
        viewHolder.textAccountId.setText(account.accountId);
        viewHolder.imgOk.setVisibility(account.isSelected ? View.VISIBLE : View.INVISIBLE);
        viewHolder.forward.setVisibility(account.forward ? View.VISIBLE : View.GONE);
        return convertView;
    }
    class ViewHolder{
        ImageView imgOk,forward;
        TextView textAccountId;

        public ViewHolder(@NonNull View view) {
            if(menuAdapter) {
                int padding_x = BasicsUtil.dp2px(context, 10);
                int padding_y = BasicsUtil.dp2px(context, 6);
                view.setPadding(padding_x, padding_y, padding_x, padding_y);
            }
            imgOk = view.findViewById(R.id.img_ok);
            textAccountId = view.findViewById(R.id.text_account_id);
            forward = view.findViewById(R.id.img_forward);
        }
    }

    /**
     * 账户数据对象
     */
    public static class Account{
        /**账户ID */
        public String accountId;
        /**是否选中*/
        public boolean isSelected;
        /**是否显示返回*/
        public boolean forward;
        /**
         * 构造方法
         * @param accountId 账户ID
         * @param isSelected 是否选中
         */
        public Account(String accountId,boolean isSelected){
            this.accountId=accountId;
            this.isSelected=isSelected;
        }
    }
}
